package br.com.fiaproupas.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class RetornaJson {
	
	private String retorno;

	public String retornaJsonClientes(){
		URL clientesURL = null;
		try {
			clientesURL = new URL("http://localhost:8081/listarCliente");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		
		return retornaJson(clientesURL);
	}
	
	public String retornaJsonPedidos(){
		URL pedidosURL = null;
		try {
			pedidosURL = new URL("http://localhost:8081/listarPedido");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		
		return retornaJson(pedidosURL);
	}
	
	public String retornaJsonUsuarios(){
		URL usuariosURL = null;
		try {
			usuariosURL = new URL("http://localhost:8081/listarUsuario");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		
		return retornaJson(usuariosURL);
	}
	
	private String retornaJson(URL url){
		URL objetoURL = url;
		BufferedReader in = null;
		URLConnection connection = null;

		try {
			connection = objetoURL.openConnection();
			in = new BufferedReader(new InputStreamReader(
					connection.getInputStream()));

			retorno = in.readLine();

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return retorno;
	}
}
